import json
import re

import requests
import time

from sqlhelper import creat, save


def get_url(page):
    data_list = []
    for i in range(0, page):
        print("-------------------------------第{}页-------------------------------".format(i + 1))
        url = 'https://www.chinahr.com/newchr/open/job/search'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        }
        data = {
            'keyWord': '',
            'localId': '1',
            'maxSalary': 'null',
            'minSalary': 'null',
            'page': i,
            'pageSize': '20'
        }
        res = requests.post(url, headers=headers, json=data).json()

        for i in res['data']['jobItems']:
            if '天' in i['salary']:
                continue

            try:
                min_salary = float(i['salary'].split('k')[0]) * 1000
                max_salary = float(i['salary'].split('-')[1].rstrip('k元/月')) * 1000
            except:
                min_salary = i['salary']
                max_salary = i['salary']
            city = i['workPlace'].split('-')[0]
            data = [i['jobName'], i['salary'], min_salary, max_salary, i['degree'], i['workExp'], i['recruitNumber'], i['workPlace'],city, i['comName'], i['industry'], i['comSize']]
            print(data)
            data_list.append(data)
    return data_list


def save_data(data_list):
    # 建表
    creat('DROP TABLE IF EXISTS zhaopin')
    sql = '''
        CREATE TABLE zhaopin (
                        id  int NOT NULL AUTO_INCREMENT,
                        jobName varchar(255) NULL,
                        salary varchar(255) NULL,
                        min_salary varchar(255) NULL,
                        max_salary varchar(255) NULL,
                        degree varchar(255) NULL,
                        workExp varchar(255) NULL,
                        recruitNumber varchar(255) NULL,
                        workPlace varchar(255) NULL,
                        city varchar(255) NULL,
                        comName varchar(255) NULL,
                        industry varchar(255) NULL,
                        comSize varchar(255) NULL,
                        

                        PRIMARY KEY (id)
                        )
    '''
    creat(sql)

    # 插入数据
    for data in data_list:
        sql = '''
                insert into zhaopin (jobName, salary, min_salary, max_salary, degree, workExp, recruitNumber, workPlace, city, comName, industry, comSize) value (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
        '''

        save(sql, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11])


if __name__ == '__main__':
    data_list = get_url(100)
    save_data(data_list)
